/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.sdk.labeling;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.ui.label.DefaultEObjectLabelProvider;
 

/**
 * An implementation for <code>ILabelProviderDelegator</code> that provides a generic labels for {@link EObject}s.
 * This can be preferably used for developing and debug purposes.
 *
 * @author Thomas Kistel
 * @version $Revision: 760 $
 */
public class DefaultObjectNameLabelProvider extends DefaultEObjectLabelProvider implements ILabelProviderDelegator {

    @Override
    public Object doGetText(Object element) {
        if(element instanceof EObject) {
            EObject eObject = (EObject) element;
            StringBuilder buf = new StringBuilder();
            buf.append(getClassName(eObject));
            
            EStructuralFeature labelFeature = getLabelFeature(eObject.eClass());
            if(labelFeature != null) {
                Object label = eObject.eGet(labelFeature);
                if(label != null) {
                    buf.append(" - ");
                    buf.append(label.toString());
                }
            }
            
            return buf.toString();
        } else {
            return getClassName(element);
        }
    }
    
    @Override
    public Object doGetImage(Object element) {
        return null;
    }
    
    private String getClassName(Object o) {
        @SuppressWarnings("rawtypes")
        Class[] interfaces = o.getClass().getInterfaces();
        if(interfaces.length == 1) {
            return interfaces[0].getSimpleName();
        }
        
        String simpleName = o.getClass().getSimpleName();
        if(simpleName.endsWith("Impl")) {
            simpleName = simpleName.substring(0, simpleName.indexOf("Impl"));
        }
        return simpleName;
    }
    
    private String getClassName(EObject o) {
        return o.eClass().getName();
    }
}
